﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFRepositoryNoDI.Data;

namespace EFRepositoryNoDI
{
	class Program
	{
		static void Main(string[] args)
		{
			// Methods in this file show basic usage but without any depedency injection baked in
		}

		private static void AllPeople()
		{
			PersonRepository personRepository = new PersonRepository();
			var people = personRepository.All();
		}

		private static void FindPerson(string lastName)
		{
			// Spin up a new PersonRepository.  
			PersonRepository personRepository = new PersonRepository();
			// The repository doesn't create a unit of work on it's own, so we need to do this
			personRepository.UnitOfWork = new EFUnitOfWork();

			// The customized PersonRepository contains a method to retrieve
			// a person by passing in their last name
			List<Person> people = personRepository.GetPersonByLastName(lastName);

			// Do some queries and updates

			// The Commit method will directly call the ObjectContext.SaveChanges method
			personRepository.UnitOfWork.Commit();
		}

		private static void SameUnitOfWork()
		{
			// Here I am creating a unit of work that will be shared by two repositories.
			// This needs to be done when you need to work with the same ObjectContext
			EFUnitOfWork efUnitOfWork = new EFUnitOfWork();

			// Create a new repository and then assign the UnitOfWork property to our unit of work
			PersonRepository personRepository = new PersonRepository();
			personRepository.UnitOfWork = efUnitOfWork;

			// Create a new repository and assign the same unit of work.  Now both repositories
			// will be working from the same ObjectContext
			AddressRepository addressRepository = new AddressRepository();
			addressRepository.UnitOfWork = efUnitOfWork;
			// Do some queries and updates


			// The commit call is made directly on the unit of work.
			// this will commit any changes made to any entity on any repository that is using
			// this unit of work
			efUnitOfWork.Commit();

		}

		private static void UsingHelpers()
		{
			// The repository helpers are there to make working with the repositories a little
			// easier.  THis way you don't have to create a unit of work by hand each time.

			// When using the helpers, the unit of work is automatically created
			// unless passed in as a parameter to the Get[Entity]Repository method
			PersonRepository personRepository = RepositoryHelper.GetPersonRepository();
			
			// The Repository GetUnitOfWork method always returns the IUnitOfWork interface
			// rather than the EFUnitOfWork concrete class
			IUnitOfWork unitOfWork = RepositoryHelper.GetUnitOfWork();

			// THe peronRepository can be created by passing in the unit of work
			personRepository = RepositoryHelper.GetPersonRepository(unitOfWork);

			// passing the unit of work here set the same unit of work so that now
			// both are using the same ObjectContext
			AddressRepository addressRepository = RepositoryHelper.GetAddressRepository(unitOfWork);
		}
	}
}
